from observable import Observable

NEXT = -2
SAME = -1

class ViewComponent(Observable):
   db = []
   @classmethod
   def _init(cls):
      cls.row = cls.col = 0
      cls.occupied = []
   def __init__(self, atr, r, c, rows, cols):
      if atr=='':
         atr=str(id(self))
      assert r>=NEXT
      assert c>=NEXT
      assert rows>0
      assert cols>0
      Observable.__init__(self)
      self.atr = atr
      self.media = View.current.media
      self.view = View.current
      assert not hasattr(View.current, atr)
      setattr (View.current, atr, self)
      cls = ViewComponent
      if r == SAME:
         self.r = cls.row
      elif r == NEXT:
         cls.row += 1
         self.r = cls.row
      else:
         self.r = r
         cls.row = r
      if c == SAME:
         self.c = cls.col
      elif c == NEXT:
         cls.col += 1
         self.c = cls.col
      else:
         self.c = c
         cls.col = c
      self.rows = rows
      self.cols = cols
      if rows>1:
         cls.row += (rows-1)
      if cols>1:
         cls.col += (cols-1)
      for r in range(self.rows):
         for c in range(self.cols):
            cell = self.r+r, self.c+c
            assert cell not in cls.occupied, self
            cls.occupied.append(cell)
      cls.db.append(self)
         
   def __str__(self):
      return '%s.%s.%s %s %s %s' % (type(View.current).__name__, self.media.__name__, type(self).__name__, self.atr, self.r, self.c)
   __repr__ = __str__

from media import media

class View(object):
   db = []
   @classmethod
   def _init(cls, self):
      cls.current = self
   def __init__(self):
      View._init(self)
      ViewComponent._init()
      self.media = media()
      self.widget = self.view = self.media.View(self)
      self.make_widgets()
   def start(self):
      self.media.start()
   def quit(self):
      self.media.quit()
   def __str__(self):
      return '%s.%s' % (type(self).__name__, self.media.__name__)
   __repr__ = __str__

class List(ViewComponent):
   def __init__(self, atr='', r=SAME, c=SAME, rows=1, cols=1):
      ViewComponent.__init__(self, atr, r, c, rows, cols)
      self.widget = self.list = self.media.List(self)
   def set(self, arg):
      self.widget.set(arg)
   def get(self):
      return self.widget.get()[0] if self.widget.get() else None 

class Label(ViewComponent):
   def __init__(self, atr='', text='', r=SAME, c=SAME, rows=1, cols=1):
      ViewComponent.__init__(self, atr, r, c, rows, cols)
      self.widget = self.label = self.media.Label(self, text)
   def set(self, text):
      self.widget.set(text)

class Entry(ViewComponent):
   def __init__(self, atr='', r=SAME, c=SAME, rows=1, cols=1):
      ViewComponent.__init__(self, atr, r, c, rows, cols)
      self.widget = self.entry = self.media.Entry(self)
   def set(self, v):
      self.widget.set(v)
   def get(self):
      return self.widget.get()

class Check(ViewComponent):
   def __init__(self, atr='', text='', r=SAME, c=SAME, rows=1, cols=1):
      ViewComponent.__init__(self, atr, r, c, rows, cols)
      self.widget = self.check = self.media.Check(self, text)
   def get(self):
      return self.widget.get()

class Button(ViewComponent):
   def __init__(self, atr='', text='', r=SAME, c=SAME, rows=1, cols=1):
      ViewComponent.__init__(self, atr, r, c, rows, cols)
      self.widget = self.button = self.media.Button(self, text)
   def set(self, text):
      self.widget.set(text)
